<template>
    <div class="table">
        <div class="container">
            <div class="handle-box">
                <el-form ref="queryFormData" :model="queryFormData" inline size="small">
                    <el-button type="primary" size="small" :disabled="multipleSelection.length !== 0 ? false : true"
                        icon="el-icon-delete" @click="delAll">批量删除</el-button>
                    <el-form-item label="歌名" style="margin-left: 10px">
                        <el-input v-model="queryFormData.songName" clearable />
                    </el-form-item>
                    <el-form-item label="歌手">
                        <el-input v-model="queryFormData.singerName" clearable />
                    </el-form-item>
                    <el-form-item label="专辑">
                        <el-input v-model="queryFormData.songAlbumName" clearable />
                    </el-form-item>
                    <el-button size="small" icon="el-icon-search" @click="search">搜索</el-button>
                </el-form>
            </div>
            <el-table v-loading="loading" :data="tableData" size="mini" style="width: 100%"
                @selection-change="handleSelectChanged">
                <el-table-column type="selection" />
                <el-table-column label="歌曲图片" width="110" align="center">
                    <template slot-scope="{row}">
                        <div class="song-img">
                            <img :src="row.pic" style="width: 100%;" />
                        </div>
                        <div class="play" v-if="row.url !== ''" @click="setSongUrl(row.url, row.name)">
                            <div v-if="row.songName === toggle">
                                <svg class="icon">
                                    <use xlink:href="#icon-zanting" />
                                </svg>
                            </div>
                            <div v-if="row.songName !== toggle">
                                <svg class="icon">
                                    <use xlink:href="#icon-bofanganniu" />
                                </svg>
                            </div>
                        </div>
                    </template>
                </el-table-column>
                <el-table-column prop="name" label="歌名" width="170" align="center" />
                <el-table-column prop="singerName" label="歌手" width="120" align="center" />
                <el-table-column prop="albumName" label="专辑" width="120" align="center" />
                <el-table-column prop="stylesStr" label="流派" width="110" align="center" />
                <el-table-column prop="lyric" label="歌词" align="center">
                    <template slot-scope="{row}">
                        <p style="height: 100px; overflow: scroll">{{ row.lyric }}</p>
                    </template>
                </el-table-column>
                <el-table-column align="center" label="评论" prop="comment" width="100">
                    <template slot-scope="{row}">
                        <el-button size="mini" :disabled="row.dataFlag === 1" @click="comment(row.songId)" icon="el-icon-edit">评论</el-button>
                    </template>
                </el-table-column>
                <el-table-column align="center" label="操作" prop="operation" width="90">
                    <template slot-scope="{row}">
                        <el-button size="mini" type="danger" @click="move(row.id)">删除</el-button>
                    </template>
                </el-table-column>
            </el-table>
            <div class="pagination">
                <el-pagination @current-change="handleCurrentChange" background layout="prev, pager, next, total"
                    :current-page="pageNum" :page-size="pageSize" :total="total" />
            </div>
        </div>
    </div>
</template>  

<script>
import { mapGetters } from 'vuex';
import '@/assets/js/iconfont.js';
import { mixin } from '../mixins';
import { listContent, delContent } from '../api/index';
export default {
    mixins: [mixin],
    data() {
        return {
            queryFormData: {
                songListId: this.$route.query.songListId,
                songName: '',
                singerName: '',
                songAlbumName: ''
            },
            tableData: [],
            pageNum: 1,
            pageSize: 10,
            total: 0,
            loading: true,
            multipleSelection: [],
            toggle: false, // 控制播放图标状态
        }
    },
    computed: {
        ...mapGetters([
            'isPlay'
        ])
    },
    mounted() {
        this.getData();
    },
    destroyed() {
        // 离开当前页面暂停播放
        this.$store.commit('setIsPlay', false);
    },
    methods: {
        getData() {
            this.loading = true;
            const { queryFormData, pageNum, pageSize } = this;
            listContent(queryFormData, pageNum, pageSize).then(res => {
                this.loading = false;
                const { data, total, success } = res;
                if (!success) {
                    this.$message.error('查询失败');
                    this.tableData = [];
                    return;
                }
                this.tableData = data.map(e => {
                    return {
                        ...e,
                        toggle: false
                    }
                });
                this.total = total;
            })
        },
        handleCurrentChange(currentPageNum) {
            this.pageNum = currentPageNum;
            this.getData();
        },
        search() {
            this.getData();
        },
        setSongUrl(url, name) {
            this.toggle = name;
            this.$store.commit('setUrl', url);
            if (this.isPlay) {
                this.$store.commit('setIsPlay', false);
            } else {
                this.$store.commit('setIsPlay', true);
            }
        },
        comment(songId) {
            this.$router.push({ path: "/Comment", query: { songId: songId } });
        },
        del(ids) {
            delContent(ids).then(res => {
                const { success } = res;
                if (success) {
                    this.$message.success("删除成功");
                    this.getData();
                } else {
                    this.$message.error("删除失败");
                }
            });
        },
        move(id) {
            this.$confirm('确定删除该记录吗?', '警告', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                this.del([id]);
            }).catch(() => {
                this.$message({
                    type: 'info',
                    message: '取消操作'
                });
            });
        },
        delAll() {
            this.$confirm('确定批量删除吗?', '警告', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                this.del(this.multipleSelection.map(e => e.id));
            }).catch(() => {
                this.$message({
                    type: 'info',
                    message: '取消操作'
                });
            });
        },
    }
}

</script>

<style scoped>
.song-img {
    width: 100%;
    height: 90px;
    margin-top: 5px;
    margin-bottom: 5px;
    overflow: hidden;
}

.play {
    position: absolute;
    z-index: 100;
    width: 80px;
    height: 80px;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    top: 18px;
    left: 15px;
}

.icon {
    width: 2em;
    height: 2em;
    color: white;
    fill: currentColor;
    overflow: hidden;
}
</style>